/**
 * fshows.com
 * Copyright (C) 2013-2019 All Rights Reserved.
 */
package com.example.fardemo.jmh.benchmark;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;

/**
 *
 * @author Liluqing
 * @version CounterBenchmark.java, v 0.1 2019-03-13 16:12
 */
@State(Scope.Group)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class CounterBenchmark {

    private AtomicLong counter;

    private LongAdder longAdder;

    private volatile long lnum;

    private volatile long lnum2;

    private long lnum3;

    @Setup
    public void up() {
        counter = new AtomicLong();
        lnum = 0;
    }

    @Benchmark
    @Group("g")
    @GroupThreads(6)
    public long inc() {
        return counter.incrementAndGet();
    }

    @Benchmark
    @Group("longAdder")
    @GroupThreads(6)
    public long incLongAdder() {
        longAdder.increment();
        return 1;
    }


//
//    @Benchmark
//    @Group("l")
//    @GroupThreads(1)
//    public long lget() {
//        return lnum;
//    }

    @Benchmark
    @Group("l")
    @GroupThreads(6)
    public long linc() {
        synchronized (this) {
            lnum++;
        }
        return lnum;
    }

    @Benchmark
    @Group("l2")
    @GroupThreads(6)
    public long linc2() {
        lnum2++;
        return lnum2;
    }

    @Benchmark
    @Group("l3")
    @GroupThreads(6)
    public long lnum3() {
        lnum3++;
        return lnum3;
    }


//    @Benchmark
//    @Group("g")
//    @GroupThreads(1)
//    public long get() {
//        return counter.get();
//    }
}